package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseAttributeMapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 分类相关接口实现类
 */
@Service
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryService {


    @Autowired
    BaseAttributeMapper baseAttributeMapper;

    @Autowired
    BaseCategory3Mapper baseCategory3Mapper;

    /**
     * 获取所有分类数据并返回一对多映射的List<JSONObject>
     *
     * @return
     */
    public List<JSONObject> getBaseCategoryList() {
        // 获取全部数据
        List<BaseCategoryView> categoryViewList = list(null);
        // 以categroy1_id为key分桶
        Map<Long, List<BaseCategoryView>> category1Map = categoryViewList.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        // 以categroy1_id为key映射一对多map并返回前端
        return category1Map.entrySet().stream().map(category1 -> {
            // 返回给前端JSONObject类型
            JSONObject category1Json = new JSONObject();
            // 以category1_key为key map遍历映射category1
            Long category1Id = category1.getKey();
            List<BaseCategoryView> category2List = category1.getValue();
            String category1Name = category2List.get(0).getCategory1Name();
            category1Json.put("categoryId", category1Id);
            category1Json.put("categoryName", category1Name);

            // 以categroy2_id为key分桶后存入category1
            Map<Long, List<BaseCategoryView>> category2Map = category2List.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            // map遍历映射category2
            List<JSONObject> category2JsonList = category2Map.entrySet().stream().map(category2 -> {
                JSONObject category2Json = new JSONObject();
                Long category2Id = category2.getKey();
                List<BaseCategoryView> category3List = category2.getValue();
                String category2Name = category3List.get(0).getCategory2Name();
                // 设置元素值
                category2Json.put("categoryId", category2Id);
                category2Json.put("categoryName", category2Name);

                // 以category3_id为key映射一对多map
                List<JSONObject> category3JsonList = category3List.stream().map(category3 -> {
                    JSONObject category3Json = new JSONObject();
                    Long category3Id = category3.getCategory3Id();
                    String category3Name = category3.getCategory3Name();
                    category3Json.put("categoryId", category3Id);
                    category3Json.put("categoryName", category3Name);
                    return category3Json;
                }).collect(Collectors.toList());
                // 将category3List存入category2
                category2Json.put("categoryChild", category3JsonList);
                return category2Json;
            }).collect(Collectors.toList());
            // 将映射好的category2List存入category1
            category1Json.put("categoryChild", category2JsonList);
            return category1Json;
        }).collect(Collectors.toList());
    }


    /**
     * 根据category1Id查询标签列表
     *
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.selectBaseAttributeByCategory1Id(category1Id);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        return baseCategory3Mapper.selectTopBaseCategory3(category1Id);
    }


    /**
     * 指定一级下所有二级和三级标签
     *
     * @param category1Id
     * @return
     */
    @Override
    public Object getBaseCategoryList(Long category1Id) {
        // 返回类型JSONObject
        JSONObject resultJson = new JSONObject();
        // 一级分类id查询二级分类list
        List<BaseCategoryView> baseCategory2Views =
                list(new LambdaQueryWrapper<BaseCategoryView>()
                        .eq(BaseCategoryView::getCategory1Id, category1Id));
        // 设置返回对象属性
        resultJson.put("categoryId", category1Id);
        resultJson.put("categoryName", baseCategory2Views.get(0).getCategory1Name());
        // 二级分类根据id分桶
        Map<Long, List<BaseCategoryView>> category2Map =
                baseCategory2Views.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        // 二级分类遍历
        List<JSONObject> category2JsonList = category2Map.entrySet().stream().map(category2 -> {
            JSONObject category2Json = new JSONObject();
            Long category2Id = category2.getKey();
            category2Json.put("categoryId", category2Id);
            List<BaseCategoryView> category3Views = category2.getValue();
            category2Json.put("categoryName", category3Views.get(0).getCategory2Name());
            // 获取三级分类 遍历映射为JSONObj
            List<JSONObject> category3JsonList = category3Views.stream().map(category3 -> {
                JSONObject category3Json = new JSONObject();
                category3Json.put("categoryId", category3.getCategory3Id());
                category3Json.put("categoryName", category3.getCategory3Name());
                return category3Json;
            }).collect(Collectors.toList());
            // 初始化二级分类返回对象
            category2Json.put("categoryChild", category3JsonList);
            return category2Json;
        }).collect(Collectors.toList());
        resultJson.put("categoryChild", category2JsonList);
        return resultJson;
    }
}
